Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.HTTP = void 0;

var e = require("../@babel/runtime/helpers/slicedToArray");

require("../@babel/runtime/helpers/Objectentries");

var r = require("../@babel/runtime/helpers/classCallCheck"), t = require("../@babel/runtime/helpers/createClass"), o = require("../utils/tool"), n = require("../config.js"), i = (getApp(), 
n.config.base_url), a = {
    "content-type": "application/json"
}, s = function() {
    function n() {
        r(this, n);
    }
    return t(n, [ {
        key: "request",
        value: function(e) {
            var r = this, t = e.url, o = e.data, n = void 0 === o ? {} : o, i = e.method, a = void 0 === i ? "GET" : i;
            return new Promise(function(e, o) {
                r._request(t, e, o, n, a);
            });
        }
    }, {
        key: "_request",
        value: function(e, r, t) {
            var o = this, n = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : {}, s = arguments.length > 4 && void 0 !== arguments[4] ? arguments[4] : "GET";
            "/ums/member/wxPhoneLogin" !== e && (a.token = wx.getStorageSync("token")), e.startsWith("http") || (e = i + e), 
            wx.request({
                url: e,
                method: s,
                data: n,
                header: a,
                success: function(e) {
                    if (e.statusCode.toString().startsWith("2")) {
                        var n = e.data.code;
                        if (401 == n) {
                            wx.removeStorageSync("LoginInfo"), t();
                            var i = o._getNowPage();
                            if (i.startsWith("/pages/login/login")) return;
                            o._loginThenRedirectTo(!0, i);
                        } else 403 == n ? wx.reLaunch({
                            url: "/pages/forbidden/forbidden"
                        }) : 500 == n ? (t(e.data), o._show_error(e.data.msg)) : r(e.data);
                    } else {
                        console.log(e), t();
                        var a = e.data.message;
                        o._show_error(a);
                    }
                },
                fail: function(e) {
                    console.log(e), t(), o._show_error(e.errMsg);
                },
                complete: function() {}
            });
        }
    }, {
        key: "_show_error",
        value: function(e) {
            e = e || "小派派正在备货", (0, o.toastToPage)(e);
        }
    }, {
        key: "_loginThenRedirectTo",
        value: function(e, r) {
            var t = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : 0, o = "/pages/login/login?fromUserID=" + t + "&redirectTo=" + encodeURIComponent(r);
            e ? wx.redirectTo({
                url: o
            }) : wx.navigateTo({
                url: o
            });
        }
    }, {
        key: "_getNowPage",
        value: function() {
            var r = getCurrentPages();
            if (r.length > 0) {
                for (var t = r[r.length - 1], o = t.route, n = t.options, i = "?", a = 0, s = Object.entries(n); a < s.length; a++) {
                    var u = e(s[a], 2), l = u[0], c = u[1];
                    i += "".concat(l, "=").concat(c, "&");
                }
                return i = i.substring(0, i.length - 1), "/".concat(o).concat(i);
            }
            return "/pages/index/index";
        }
    } ]), n;
}();

exports.HTTP = s;